package com.assembly;

import java.util.concurrent.RecursiveTask;

public class ForkJoinExample extends RecursiveTask<Integer> {


    private final int threshold=5;
    private int first;
    private int last;

    public ForkJoinExample(int first,int last){
        this.first=first;
        this.last=last;
    }

    /*
    ForkJoin 主要用于进行计算中，和MapReduce原理类似，都是把大的计算任务拆分成多个小任务并行计算
     */
    @Override
    protected Integer compute(){
        int result=0;
        if (last -first<= threshold){
            //任务足够小则直接计算
            for(int i=first;i<=last;i++){
                result+=i;
            }
        }else
        {
            //拆分成小任务
            int middle=first+(last-first)/2;
            ForkJoinExample leftTask=new ForkJoinExample(first,middle);
            ForkJoinExample rightTask=new ForkJoinExample(middle+1,last);
            leftTask.fork();
            rightTask.fork();
            result=leftTask.join()+rightTask.join();
        }

        return result;
    }

    public static void main(String[] args) {
        ForkJoinExample example=new ForkJoinExample(1,1000);

    }

}
